<!-- 原料类别 损益分析 -->
<template>
  <div>
    <el-form class="const_top" :model="queryParams" size="small" :inline="true">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="" style="height: 40px">
            <el-select
              v-model="byMonth.relateId"
              placeholder="请选择部门"
              clearable
              @change="deptChange"
            >
              <el-option
                v-for="dict in deptOptions"
                :key="dict.relateId"
                :label="dict.label"
                :value="dict.relateId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" style="height: 40px">
            <el-date-picker
              clearable
              v-model="queryParams.createdTime"
              type="month"
              placeholder="选择月"
              @change="createdTimeChange"
              :disabled="mouday"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="" style="height: 40px">
            <el-select
              v-model="queryParams.pandianCode"
              placeholder="1号至10号"
              clearable
              :disabled="mount"
            >
              <el-option
                v-for="dict in NewDropDownList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="汇总维度" style="height: 40px"> </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery"
              >查询
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" :row-click="handleRowClick">
          <el-table-column align="center" label="序号" type="index" width="50">
          </el-table-column>
          <!--  -->
          <el-table-column label="原料类别" prop="pname" align="center">
          </el-table-column>
          <el-table-column
            v-for="(item, index) in dataOne"
            :key="index"
            :label="item.label"
            :prop="item.children ? '' : item.props"
            align="center"
          >
            <template v-if="item.children">
              <el-table-column
                v-for="(ite, ind) in item.children"
                :key="ind"
                :label="ite.label"
                :prop="ite.props"
                align="center"
              ></el-table-column>
            </template>
          </el-table-column>
          <el-table-column label="操作" props="" align="center">
            <template slot-scope="scope">
              <div>
                <el-button size="mini" type="text" @click="Godept(scope.row)"
                  >部门分析</el-button
                >
                <el-button size="mini" type="text" @click="Gozilei(scope.row)"
                  >子类分析</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// import {  } from "@/api/system/cost.js";
import { getPanDianCode, FindShopBom } from "@/api/system/stock";
import { topcatePage } from "@/api/system/cost";
export default {
  props: {
    dataOne: {
      type: Array,
    },
  },
  data() {
    return {
      queryParams: {},
      deptOptions: [],
      DropDownList: [],
      NewDropDownList: [],
      byMonth: {},
      mouday: true,
      mount: true,
      defaultProps: {
        children: "children",
        label: "label",
      },
      tableData: [],
    };
  },
  created() {
    FindShopBom().then((res) => {
      // console.log(res);
      if (res.code == 200) {
        let ovj = res.data.filter((ite) => {
          if (ite.relateId !== null) {
            delete ite.children;
            return ite;
          }
        });
        if (ovj.length > 1) {
          this.mouday = true;
        }
        this.deptOptions = ovj;
      }
    });
  },

  methods: {
    handleQuery() {
      if (this.byMonth.relateId) {
        this.queryParams.shopCode = this.byMonth.relateId;
      }
      topcatePage(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
        }
      });
    },
    getANENd() {
      topcatePage(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data;
        }
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      this.queryParams.shopCode = data.relateId;
      this.getANENd();
    },
    // 部门分析
    Godept(v) {
      // sectoralAnalysis  部门
      this.$router.push({
        path: "/sectoralAnalysis",
        query: {
          pandianCode: this.queryParams.pandianCode,
          shopCode: this.queryParams.shopCode,
          stapType: v.stapType,
        },
      });
    },
    // 子类分析
    Gozilei(v) {
      // SubclassAnalysis 子类
      this.$router.push({
        path: "/SubclassAnalysis",
        query: {
          pandianCode: this.queryParams.pandianCode,
          shopCode: this.queryParams.shopCode,
          stapType: v.stapType,
        },
      });
    },
    createdTimeChange(v) {
      let data = new Date(v);
      let y = data.getFullYear();
      let m = data.getMonth() + 1;
      this.byMonth.pandianData = y + String(m).padStart(2, "0");
      getPanDianCode(this.byMonth).then((res) => {
        if (res.code == 200) {
          this.DropDownList = res.data;
          this.NewDropDownList = [];
          this.DropDownList.forEach((element) => {
            let label =
              new Date(element.startTime).getDate() +
              "号-" +
              new Date(element.endTime).getDate() +
              "号";
            this.NewDropDownList.push({
              value: element.pandianCode,
              label: label,
            });
          });
          if (res.data != []) {
            this.mount = false;
          }
        }
      });
    },
    deptChange(v) {
      if (v) {
        this.mouday = false;
      } else {
        this.mouday = true;
      }
    },
    handleRowClick() {},
  },
  mounted() {
    // 部门分析
    // SubclassAnalysis
    // 子类分析
    // this.$router.push("/sectoralAnalysis");
  },
};
</script>

<style lang="scss" scoped>
.clas_All {
  margin-left: 20px;
  margin-right: 20px;
}
</style>
